package tianwang.utils.punctuator;

import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

import tianwang.utils.stanford.StanfordUtils;
import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.ling.Word;
import edu.stanford.nlp.process.DocumentPreprocessor;

public class PunctuatorStanfordImpl implements Punctuator{

	public String[] punctuate(String text) {

		DocumentPreprocessor documentPreprocessor  = StanfordUtils.getDocumentPreprocessor();

		String regex = "[\\W+]";
		Pattern p = Pattern.compile(regex);

		List<List<? extends HasWord>> list = documentPreprocessor.getSentencesFromText(new StringReader(text));
		String[] sentences = new String[list.size()];
		int index = 0;
		for ( Iterator itr = list.iterator(); itr.hasNext();){
			List jist = (List) itr.next();

			StringBuilder sb = new StringBuilder();
			for ( Iterator jtr = jist.iterator(); jtr.hasNext();){
				Word word = (Word) jtr.next();
				String value = word.value();
				if ( value.length() <= 1 && p.matcher(value).matches()) {
					sb.append(value);
				} else {
					sb.append(' ').append(value);
				}
			}
			sentences[index++] = sb.toString().trim();
			sb.delete(0, sb.length());
		}
		
		return sentences;
	}
}
